﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Controls;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using INTEX.DataBase;
namespace WPFExcelReport
{

    public class ITXSETUPData : BaseFunction
    {
        GB2312Convert8859P1 gb2312 = new GB2312Convert8859P1();
        INTEXDBC INTEXDBC;
        SETINTEXDB INTEXDB;
        string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\ITXSETUP.xls");
        public ITXSETUPData()
            : base()
        {
            if (System.IO.File.Exists(filePath))
            {
                exAgent = new ExcelX.ExcelLib.ExcelAgent(filePath);
            }
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            INTEXDB = new SETINTEXDB();
        }
        public ITXSETUPData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
            if (System.IO.File.Exists(filePath))
            {
                exAgent = new ExcelX.ExcelLib.ExcelAgent(filePath);
            }
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            INTEXDB = new SETINTEXDB();
        }
        public ITXSETUPData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            if (System.IO.File.Exists(filePath))
            {
                exAgent = new ExcelX.ExcelLib.ExcelAgent(filePath);
            }
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            INTEXDB = new SETINTEXDB();
        }
        public DataSet LoadEnv(string client)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                if (INTEXDBC == null)
                {
                    INTEXDBC = new INTEXDBC(OracleDB, proxy);
                }
                DataRow dwData = INTEXDB.HRSTAMM.NewRow();
                dwData["HRMANDANT"] = client;
                setResult = INTEXDBC.QueryHRSTAMM(dwData, 4);
                setReturn.Merge(setResult);
                dwData = INTEXDB.ICC_FUNCTIONS.NewRow();
                dwData["MANDANT"] = client;
                setResult = INTEXDBC.QueryICC_FUNCTIONS(dwData, 9999);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet ReSetData()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet settemp = new DataSet();

            ArrayList tables = new ArrayList();
            string fileroot = Path.Combine(PathHelper.ApplicationPath, @"Documents");
            string filePath = "";

            string[] files = System.IO.Directory.GetFiles(fileroot);
            //string filepath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\ITXSETUP.xls");
            string ShortName = "";
            try
            {
                foreach (string file in files)
                {
                    if (!file.EndsWith("SETUP.xls"))
                        continue;
                    if (exAgent == null)
                    {
                        exAgent = new ExcelX.ExcelLib.ExcelAgent(file);
                    }
                    setResult = exAgent.LoadDataTableDef(file, "");

                    foreach (DataTable tbTemp in setResult.Tables)
                    {
                        string tbName = tbTemp.TableName;
                        settemp = new DataSet();
                        settemp.DataSetName = "SETUP";
                        settemp.Merge(tbTemp);
                        filePath = fileroot + "\\" + tbName + ".XML";
                        if (System.IO.File.Exists(filePath))
                        {
                            System.IO.File.Delete(filePath);
                        }
                        settemp.WriteXml(filePath);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                exAgent.KillExcelProcess();
                exAgent.Close();
            }
            return setReturn;
        }
        public DataSet LoadFunction(string client = "00")
        {
            if (INTEXDBC == null)
            {
                INTEXDBC = new INTEXDBC(OracleDB, proxy);
            }
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                ArrayList tables = new ArrayList();
                string filePath = "";
                string fileroot = Path.Combine(PathHelper.ApplicationPath, @"Documents");
                string[] files = System.IO.Directory.GetFiles(fileroot);
                filePath = fileroot + "\\FUNCTION.XML";
                if (System.IO.File.Exists(filePath))
                {
                    setResult = new DataSet();
                    setResult.ReadXml(filePath);
                    if (setResult.Tables.Count > 0)
                    {
                        setResult.Tables[0].TableName = "ICC_FUNCTIONS";
                    }
                    setReturn.Merge(setResult);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadITXFunction(string client)
        {
            if (INTEXDBC == null)
            {
                INTEXDBC = new INTEXDBC(OracleDB, proxy);
            }
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = INTEXDB.ICC_FUNCTIONS.NewRow();
                dwData["MANDANT"] = client;
                setResult = INTEXDBC.QueryICC_FUNCTIONS(dwData, 9999);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void SaveFunctionSetup(DataSet src, string client)
        {
            try
            {                

                foreach (DataRow rw in src.Tables["ICC_FUNCTIONS"].Rows)
                {
                   
                    rw["MANDANT"] = client;
                  

                    INTEXDBC.SaveICC_FUNCTIONS(rw);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet LoadFolder(string client)
        {
            if (INTEXDBC == null)
            {
                INTEXDBC = new INTEXDBC(OracleDB, proxy);
            }
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                ArrayList tables = new ArrayList();
                string filePath = "";
                string fileroot = Path.Combine(PathHelper.ApplicationPath, @"Documents");
                string[] files = System.IO.Directory.GetFiles(fileroot);


                filePath = fileroot + "\\FOLDERS.XML";
                if (System.IO.File.Exists(filePath))
                {
                    setResult = new DataSet();
                    setResult.ReadXml(filePath);
                    if (setResult.Tables.Count > 0)
                    {
                        setResult.Tables[0].TableName = "ICC_FOLDERS";
                    }
                    setReturn.Merge(setResult);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadITXFolder(string client)
        {
            if (INTEXDBC == null)
            {
                INTEXDBC = new INTEXDBC(OracleDB, proxy);
            }
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = INTEXDB.ICC_FOLDERS.NewRow();
                dwData["MANDANT"] = client;
                setResult = INTEXDBC.QueryICC_FOLDERS(dwData, 9999);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void SaveFolderSetup(DataSet src, string client)
        {
            try
            {
                 /*
                if (src.Tables["ICC_FOLDERS"].Rows.Count > 0)
                {
                    INTEXDBC.DeleteICC_FOLDERS(client, "");
                }
                */
                foreach (DataRow rw in src.Tables["ICC_FOLDERS"].Rows)
                {       
                    rw["MANDANT"] = client;
                  
                    rw["ROOT"] = gb2312.ConvertBack(rw["ROOT"].ToString(), typeof(string), null, null);
                    rw["SUB"] = gb2312.ConvertBack(rw["SUB"].ToString(), typeof(string), null, null);
                    INTEXDBC.SaveICC_FOLDERS(rw);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet LoadMenu(string client = "00")
        {
            if (INTEXDBC == null)
            {
                INTEXDBC = new INTEXDBC(OracleDB, proxy);
            }
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                ArrayList tables = new ArrayList();
                string filePath = "";
                string fileroot = Path.Combine(PathHelper.ApplicationPath, @"Documents");
                string[] files = System.IO.Directory.GetFiles(fileroot);

                foreach (string file in files)
                {
                    if (!file.Contains("MENU"))
                        continue;
                    if (System.IO.File.Exists(file))
                    {
                        setResult = new DataSet();
                        setResult.ReadXml(file);
                        if (setResult.Tables.Count > 0)
                        {
                            setResult.Tables[0].TableName = "ICC_MENUDEF";
                        }
                        setReturn.Merge(setResult);
                    }

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {

            }
            return setReturn;
        }
        public DataSet LoadITXMenu(string client, string company)
        {
            if (INTEXDBC == null)
            {
                INTEXDBC = new INTEXDBC(OracleDB, proxy);
            }
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = INTEXDB.ICC_MENUDEF.NewRow();
                dwData["MANDANT"] = client;
                dwData["COMPANY"] = company;
                setResult = INTEXDBC.QueryICC_MENUDEF(dwData, 9999);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void SaveMenuSetup(DataSet src, string client)
        {
            try
            {

              
                string userid = "";
                string processed = "";
                string company = "";
                foreach (DataRow rw in src.Tables["ICC_MENUDEF"].Rows)
                {
                    /*userid = rw["USERID"].ToString();
                    userid = Helpers.Helper.Left(userid + "                 ", 8);
                    if (!processed.Contains(userid + ";"))
                    {
                        processed += userid + ";";                    
                        INTEXDBC.DeleteICC_MENUDEF(client, company, userid);
                    }*/            
                    rw["MANDANT"] = client;               
                    //rw["HEADER"] = gb2312.ConvertBack(rw["HEADER"].ToString(), typeof(string), null, null);
                    company = rw["COMPANY"].ToString();
                    INTEXDBC.SaveICC_MENUDEF(rw);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void DeleteHiddenMenu()
        {
            try
            { 
              
                    INTEXDBC.DeleteICC_MENUDEF(null,1);
        
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}